
package org.apache.solr.util.plugin;

import java.util.Map;
import org.apache.solr.util.DOMUtil;
import org.w3c.dom.Node;

/**
 * @since solr 1.3
 */
public class MapPluginLoader<T extends MapInitializedPlugin> extends AbstractPluginLoader<T> {

    private final Map<String, T> registry;

    public MapPluginLoader(String name, Class<T> pluginClassType, Map<String, T> map) {
        super(name, pluginClassType);
        registry = map;
    }

    @Override
    protected void init(T plugin, Node node) throws Exception {
        Map<String, String> params = DOMUtil.toMapExcept(node.getAttributes(), "name", "class");
        plugin.init(params);
    }

    @Override
    protected T register(String name, T plugin) throws Exception {
        if (registry != null) {
            return registry.put(name, plugin);
        }
        return null;
    }
}
